﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PlaceHR.Processor.Processor
{
    public class VacationEngine : EngineBase
    {
        public VacationEngine(int year, int month)
        {
            this.Year = year;
            this.Month = month;
        }

        public override string EngineName
        {
            get
            {
                return Constants.VacationTypeCode;
            }
        }

        public override void Run()
        {
            Record = HRDBContext.ProcessorRecord.FirstOrDefault(
                    p => p.Year == this.Year 
                       && p.Month == this.Month
                       && p.ProcessorTypeCode != null
                       && p.ProcessorTypeCode.Equals(EngineName, StringComparison.OrdinalIgnoreCase));

            if (Record == null)
            {
                Record = new ProcessorRecord
                {
                    ProcessorTypeCode = EngineName,
                    Year = this.Year,
                    Month = this.Month,
                    ProcessorStatusCode = ProcessorStatusCode.Pending,
                    InsertDate = DateTime.Now,
                    UpdateDate = DateTime.Now,
                    UpdateBy = -1
                };

                HRDBContext.ProcessorRecord.AddObject(Record);
                HRDBContext.SaveChanges();
            }

            if (Record.ProcessorStatusCode.Equals(ProcessorStatusCode.Pending, StringComparison.OrdinalIgnoreCase) || this.ForceRecalculate)
            {
                Console.WriteLine(string.Format("开始计算{0}年{1}月假期...", this.Year, this.Month));
                this.ProcessEmployeeVacation();

                Record.ProcessorStatusCode = ProcessorStatusCode.Succeed;
                Record.UpdateDate = DateTime.Now;
                HRDBContext.SaveChanges();

                this.LogEvent("Vacation Auto Engine", string.Format("{0}年{1}月份假期计算完毕！", this.Year, this.Month));
            }
            else
            {
                Console.WriteLine(string.Format("{0}年{1}月假期数据已存在，退出计算！", this.Year, this.Month));
            }

        }

        #region Private Methods

        private void ProcessEmployeeVacation()
        {
            var employeeList = HRDBContext.Employee.Where(p => !p.IsDeleted
                && p.StatusCode != null
                && p.StatusCode.Equals(EmployeeStatusCode.Regular, StringComparison.OrdinalIgnoreCase)).ToList();

            if (employeeList == null || employeeList.Count < 1)
            {
                return;
            }

            foreach (var employee in employeeList)
            {
                try
                {
                    var processor = new VacationProcessor();
                    processor.Init(employee, this);
                    processor.Process();
                }
                catch (Exception ex)
                {
                    this.LogEvent("Vacation Calculate Error", ex.Message);
                }

            }
        }

        #endregion

    }
}
